package com.apps.Config;


import com.apps.globals.advent.service.IKupaAdventService;
import com.apps.globals.foot.service.IKupaFooterColumnService;
import com.apps.globals.global.service.IKupaGlobalService;
import com.apps.goods.service.IKupaGoodsCatService;
import com.apps.interceptor.GlobalInterceptor;
import com.apps.sworld.sConfig;
import com.apps.thirdparty.upload.fileupload.config.IKupaUpLoadConfig;
import com.apps.vip.service.IKupaVipGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @description: web拦截配置
 * @author: YuXD
 * @create: 2019-02-11 12:27
 **/
@Configuration
public class WebAuthConfig implements sConfig {

    @Autowired(required = false)
    private IKupaFooterColumnService footerColumnService;
    @Autowired(required = false)
    private IKupaGlobalService globalService;
    @Autowired(required = false)
    private IKupaAdventService adventService;
    @Autowired(required = false)
    private IKupaGoodsCatService goodsCatService;

    @Autowired(required = false)
    private IKupaUpLoadConfig IKupaUpLoadConfig;

    @Autowired(required = false)
    private IKupaVipGoodsService vipGoodsService;


    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            /**
             * 跨域支持
             * @param registry
             */
            @Override
            public void addCorsMappings(CorsRegistry registry) {
//                registry.addMapping("/**");
            }

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                // 只对非静态资源进行全局拦截
                GlobalInterceptor globalInterceptor = new GlobalInterceptor(footerColumnService,
                                                                            globalService,
                                                                            adventService,
                                                                            goodsCatService,
                                                                            vipGoodsService);
                registry.addInterceptor(globalInterceptor);//注册全局拦截器
            }

            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry)
            {
                //配置上传资源访问路径
                registry.addResourceHandler("/"+ IKupaUpLoadConfig.getRootpath()+"/**").addResourceLocations("file:"+ IKupaUpLoadConfig.getSysRootpath()+"/");
                //配置静态资源访问路径
                registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
                //配置swagger路径
                registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
                registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
            }

            /**
             * 配置首页
             * @param registry
             */
            @Override
            public void addViewControllers(ViewControllerRegistry registry)
            {
                //设置主页
                registry.addViewController("/").setViewName("redirect:/page/goods/1/1/index.html");
                //设置优先级
                registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
            }
        };
    }

}
